import path from 'path'
import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import { ViteImageOptimizer } from 'vite-plugin-image-optimizer'
import AutoImport from 'unplugin-auto-import/vite'
import legacy from '@vitejs/plugin-legacy'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, './')

  return {
    base: env.VITE_BASE_URL,
    plugins: [
      vue(),
      vueJsx(),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: 'less' // css in js
          })
        ],
        version: 3
      }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]'
      }),
      ViteImageOptimizer(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia'], //自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入
        dts: 'auto-import.d.ts',
        eslintrc: {
          enabled: false, // Default `false`
          filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
          globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
        }
      }),
      legacy({
        targets: ['ie > 11'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime']
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        lodash: 'lodash-es'
      }
    },
    server: {
      host: '0.0.0.0',
      port: 3000,
      cors: true,
      proxy: {
        '/dev/api': {
          target: 'http://10.10.22.133:8580',
          // target: 'http://10.10.25.71',
          // target: 'http://10.10.25.83',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev\/api/, '')
        }
      },
      hmr: { overlay: false }
    },
    build: {
      outDir: env.VITE_OUT_DIR,
      assetsDir: 'assets',
      rollupOptions: {
        output: {
          // assetFileNames: 'assets/images/[name]-[hash][extname]',
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: (assetInfo) => {
            if (
              assetInfo.type === 'asset' &&
              /\.(jpe?g|png|gif|svg)$/i.test(assetInfo.name || '')
            ) {
              return 'assets/images/[name]-[hash][extname]'
            }
            if (
              assetInfo.type === 'asset' &&
              /\.(ttf|woff|woff2|eot)$/i.test(assetInfo.name || '')
            ) {
              return 'assets/fonts/[name]-[hash][extname]'
            }
            return 'assets/[ext]/[name]-[hash][extname]'
          },
          manualChunks: {
            echarts: ['echarts'],
            lodash: ['lodash', 'lodash-es'],
            vue: ['vue', 'vue-router'],
            antd: ['ant-design-vue']
          }
        }
      }
    }
  }
})
